package org.baitu.zzim.decoder;

import io.netty.channel.ChannelHandler.Sharable;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.baitu.zzim.model.ChatMessage;
import org.baitu.zzim.pojo.chat.ChatMessageOuterClass;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Sharable
public class MessageDecoder extends MessageToMessageDecoder<TextWebSocketFrame> {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void decode(ChannelHandlerContext ctx, TextWebSocketFrame frame, List<Object> out) throws Exception {
        String json = frame.text();
        ChatMessage message = objectMapper.readValue(json, ChatMessage.class);
        out.add(message);
    }
}